import Vue from 'vue'
import Vuex from 'Vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userId: localStorage.userId || '',
    voteId: localStorage.voteId || '',
    userPassword: '',
    themeId: localStorage.themeId || '',
    // themeName: '',
    vStatus: false,
    canPoll: true
  },
  actions: {
    Login: ({commit}, user) => {
      commit('Login', user)
    },
    LoginVote: ({commit}, voteId) => {
      commit('LoginVote', voteId)
    },
    themeId (ctx, themeId) {
      ctx.commit('themeId', themeId)
    },
    changeVstatus (ctx, bool) {
      ctx.commit('changeVstatus', bool)
    },
    changeCanPoll (ctx, params) {
      ctx.commit('changeCanPoll', params)
    }
    // curvoteTheme (ctx, theme) {
    //   ctx.commit('curvoteTheme', theme)
    // }
  },
  mutations: {
    Login (state, user) {
      state.userId = user.id
      state.userPassword = user.pwd
      // console.log(state.userPassword)
      try {
        localStorage.userId = user.id
      } catch (e) {
        console.log(e)
      }
    },
    LoginVote (state, voteId) {
      state.voteId = voteId
      try {
        localStorage.voteId = voteId
      } catch (e) {
        console.log(e)
      }
    },
    themeId: (state, themeId) => {
      state.themeId = themeId
      try {
        // console.log(themeId)
        localStorage.themeId = themeId
      } catch (e) {
        console.log(e)
      }
    },
    changeVstatus: (state, bool) => {
      state.vStatus = bool
      // console.log(bool)
    },
    changeCanPoll: (state, params) => {
      state.canPoll = params
    }
    // curvoteTheme: (state, theme) => {
    //   state.themeName = theme
    // }
  }
})
